package com.bazhua.bazo.service.Impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bazhua.bazo.repository.entity.ContextService;
import com.bazhua.bazo.repository.entity.WechatUser;
import com.bazhua.bazo.repository.mapper.ContextServiceMapper;
import com.bazhua.bazo.repository.model.ContextModel;
import com.bazhua.bazo.repository.model.ContextSearch;
import com.bazhua.bazo.repository.model.ContextServiceModel;
import com.bazhua.bazo.repository.model.UserType;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.repository.model.result.ResultUtils;
import com.bazhua.bazo.service.IContextService;
import com.bazhua.bazo.service.IWechatUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContextServiceImpl extends ServiceImpl<ContextServiceMapper, ContextService> implements IContextService {

    @Override
    public CommonResult AddContext(ContextServiceModel model) {
        if (model.getContext() == null || StringUtils.isBlank(model.getContext()))
            return ResultUtils.error(90210, "内容不能为空");
        if (model.getContextType() == null || model.getContextType() <= 0)
            return ResultUtils.error(90210, "服务类型错误");
        if (StringUtils.isBlank(model.getContextName()))
            return ResultUtils.error(90210, "联系人不能为空");
        if (StringUtils.isBlank(model.getContextPhone()))
            return ResultUtils.error(90210, "联系电话不能为空");
        ContextService entity = new ContextService();
        entity.setContext(model.getContext());
        entity.setContextName(model.getContextName());
        entity.setContextPhone(model.getContextPhone());
        entity.setContextType(model.getContextType());
        entity.setUserId(model.getUserId());
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
        return ResultUtils.ok();
    }

    @Autowired
    private IWechatUserService wechatUserService;

    @Override
    public PageListResult GetContextPage(ContextSearch search) {

        QueryWrapper<ContextService> wrapper = new QueryWrapper<>();
        if (search.getContextType() != null && search.getContextType() > 0)
            wrapper.eq("context_type", search.getContextType());
        wrapper.orderByDesc("create_time");


        if (search.getUserId() != null && search.getUserId() > 0) {
            WechatUser user = wechatUserService.getById(search.getUserId());
            if (user == null || user.getId() <= 0)
                return ResultUtils.pageError(90200, "商户不存在");

            if (!user.getUserType().equals(UserType.ShopAdmin)) {
                wrapper.eq("user_id", search.getUserId());
            }
        }

        if (!StringUtils.isBlank(search.getPhone()) && (search.getUserId() == null && search.getUserId() <= 0)) {
            QueryWrapper<WechatUser> userWrapper = new QueryWrapper<>();
            WechatUser user = wechatUserService.getOne(userWrapper);
            wrapper.eq("shop_phone", user.getPhone());
        }

        Page<ContextService> page = new Page<>();
        page.setSize(search.getPageSize());
        page.setPages(search.getPageNum());
        page = baseMapper.selectPage(page, wrapper);

        if (page.getRecords() != null && page.getRecords().size() > 0) {
            Page<ContextModel> modelPage = new Page<ContextModel>();
            modelPage.setCurrent(page.getCurrent());
            modelPage.setSize(page.getSize());
            modelPage.setTotal(page.getTotal());
            modelPage.setPages(page.getPages());
            modelPage.setMaxLimit(page.getMaxLimit());
            List<ContextModel> list = new ArrayList<>();
            for (ContextService info : page.getRecords()) {
                ContextModel item = new ContextModel();
                item.setContext(info.getContext());
                item.setContextType(info.getContextType());
                item.setUserName(info.getContextName());
                item.setUserPhone(info.getContextPhone());
                item.setId(info.getId());
                item.setCreateTime(info.getCreateTime());
                WechatUser user = wechatUserService.getById(info.getUserId());
                item.setUserId(user.getId());
                item.setUserName(user.getUserName());
                item.setUserPhone(user.getPhone());
                list.add(item);
            }
            modelPage.setRecords(list);
            return ResultUtils.pageOk(modelPage);
        } else
            return ResultUtils.pageError(90200, "查询数据为空");
    }
}
